//
//  ABObject.m
//  afnetworkingtest
//
//  Created by appbegin on 14-2-8.
//  Copyright (c) 2014年 appbegin. All rights reserved.
//

#import "SEObject.h"
#import "SEExcute.h"
#import "AFNetworking.h"
#import "JQProgressHUD.h"
#import "VDJConstant.h"

#define kNetWorkingString  @"网络异常, 请检查网络"

#define HOST_API @"http://v2.api.vdianjing.com/"

//https://service.lianluoyuan.com:8081/

@interface SEObject(){
}

@end;
@implementation SEObject
+ (SEObject *)objectWithClassName:(NSString *)className
{
    SEObject *object = [[SEObject alloc] init];
    object.className = className;
    return object;
}

- (NSMutableDictionary *)saveDataDic
{
    if (_saveDataDic == nil) {
        _saveDataDic = [[NSMutableDictionary alloc] init];
    }
    return _saveDataDic;
}

- (id)initWithClassName:(NSString *)newClassName
{
    return self;
}

- (void)setObject:(id)value forKey:(NSString *)aKey
{
    //value and aKey are not allowed nil.
    [self.saveDataDic setObject:value forKey:aKey];
}

- (id)objectForKey:(NSString *)key
{
    return [self.saveDataDic valueForKey:key];
}

- (BOOL)save
{
    MyLog(@"save");
    return NO;
}

- (BOOL)save:(NSError **)error
{
    MyLog(@"save with error");
    return NO;
}

- (void)saveInBackground
{
    SEExcute *excute = [[SEExcute alloc] init];
    NSString *path = [NSString stringWithFormat:@"%@%@",HOST_API,self.className];
    [excute excutePostWithPath:path withParam:self.saveDataDic onSuccess:^(NSDictionary *results) {
        MyLog(@"成功!! = %@",results);
    } onFailure:^(NSError *error) {
        MyLog(@"失败!! = %@",error);
    }];
}

- (void)saveInBackgroundArrayOnSuccess:(ABResultsArraySuccessBlock)successBlock onFailureBlock:(ABFailureBlock)failureBlock
{
    NSString *path = [NSString stringWithFormat:@"%@%@",HOST_API,self.className];
    SEExcute * excute = [[SEExcute alloc] init];
    [excute excutePostReturnArrayWith:path withParam:self.saveDataDic onSuccess:^(NSArray *dataDic) {
        successBlock(dataDic);
    } onFailure:^(NSError *error) {
        MyLog(@"error = %@",error);
    }];
}

- (void)saveInbackgroundOnSuccess:(ABSaveSuccessBlock)successBlock onFailureBlock:(ABFailureBlock)failureBlock
{
    MyLog(@"saveInbackgroundOnSuccess");;
    [self saveInbackgroundWithHasHeader:NO OnSuccess:^(SEObject *object) {
        successBlock(object);
    } onFailureBlock:^(NSError *error) {
        failureBlock(error);
    }];
}

- (void)saveInbackgroundWithHasHeader:(BOOL)hasToken OnSuccess:(ABSaveSuccessBlock)successBlock onFailureBlock:(ABFailureBlock)failureBlock
{
    //网络断了 则不能保存
    AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager sharedManager];
    [manager startMonitoring];
    [manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        
        switch (status) {
            case AFNetworkReachabilityStatusNotReachable:
                [JQProgressHUD showErrorMessage:kNetWorkingString];
                break;
            case AFNetworkReachabilityStatusReachableViaWWAN:
            case AFNetworkReachabilityStatusReachableViaWiFi:
            {
                SEExcute *excute = [[SEExcute alloc] init];
                NSString *path = [NSString stringWithFormat:@"%@%@",HOST_API,self.className];
                [excute excutePostWithPath:path withParam:self.saveDataDic hasHeader:hasToken onSuccess:^(NSDictionary *dataDic) {
                    self.saveDataDic = [[NSMutableDictionary alloc] initWithDictionary:dataDic];
                    successBlock(self);
                } onFailure:^(NSError *error) {
                    failureBlock(error);
                }];
            }
                
            default:
                break;
        }
    }];
}

-(void)saveInbackgroundWithHasHeader:(BOOL)hasToken withNoHud:(BOOL)noHud OnSuccess:(ABSaveSuccessBlock)successBlock onFailureBlock:(ABFailureBlock)failureBlock
{
    //网络断了 则不能保存
    AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager sharedManager];
    [manager startMonitoring];
    [manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        
        switch (status) {
            case AFNetworkReachabilityStatusNotReachable:
                [JQProgressHUD showErrorMessage:kNetWorkingString];
                break;
            case AFNetworkReachabilityStatusReachableViaWWAN:
            case AFNetworkReachabilityStatusReachableViaWiFi:
            {
                SEExcute *excute = [[SEExcute alloc] init];
                NSString *path = [NSString stringWithFormat:@"%@%@",HOST_API,self.className];
                
                [excute excutePostWithPath:path withParam:self.saveDataDic hasHeader:hasToken noHud:noHud onSuccess:^(NSDictionary *dataDic) {
                    self.saveDataDic = [[NSMutableDictionary alloc] initWithDictionary:dataDic];
                    successBlock(self);
                } onFailure:^(NSError *error) {
                    failureBlock(error);
                }];
            }
                
            default:
                break;
        }
    }];
}

// AFMultipartFormData
-(void)saveInbackgroundFormDataWithHasHeader:(BOOL)hasToken withNoHud:(BOOL)noHud OnSuccess:(ABSaveSuccessBlock)successBlock onFailureBlock:(ABFailureBlock)failureBlock
{
    //网络断了 则不能保存
    AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager sharedManager];
    [manager startMonitoring];
    [manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        
        switch (status) {
            case AFNetworkReachabilityStatusNotReachable:
                [JQProgressHUD showErrorMessage:kNetWorkingString];
                break;
            case AFNetworkReachabilityStatusReachableViaWWAN:
            case AFNetworkReachabilityStatusReachableViaWiFi:
            {
                SEExcute *excute = [[SEExcute alloc] init];
                NSString *path = [NSString stringWithFormat:@"%@%@",HOST_API,self.className];
                
                [excute excuteFormDataPostWithPath:path withParam:self.saveDataDic hasHeader:hasToken onSuccess:^(NSDictionary *dataDic) {
                    self.saveDataDic = [[NSMutableDictionary alloc] initWithDictionary:dataDic];
                    successBlock(self);
                } onFailure:^(NSError *error) {
                    failureBlock(error);
                }];
            }
                
            default:
                break;
        }
    }];
}

-(void)saveInbackgroundUpdataUserHeaderWithHasHeader:(BOOL)hasToken withFile:(NSData *)file OnSuccess:(ABSaveSuccessBlock)successBlock onFailureBlock:(ABFailureBlock)failureBlock
{
    //网络断了 则不能保存
    AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager sharedManager];
    [manager startMonitoring];
    [manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        
        switch (status) {
            case AFNetworkReachabilityStatusNotReachable:
                [JQProgressHUD showErrorMessage:kNetWorkingString];
                break;
            case AFNetworkReachabilityStatusReachableViaWWAN:
            case AFNetworkReachabilityStatusReachableViaWiFi:
            {
                SEExcute *excute = [[SEExcute alloc] init];
                NSString *path = [NSString stringWithFormat:@"%@%@",HOST_API,self.className];
                
                [excute excuteUserHeaderPostWithPath:path withParam:self.saveDataDic withFile:file onSuccess:^(NSDictionary *dataDic) {
                    
                    self.saveDataDic = [[NSMutableDictionary alloc] initWithDictionary:dataDic];
                    successBlock(self);
                } onFailure:^(NSError *error) {
                    
                    failureBlock(error);
                }];
            }
                
            default:
                break;
        }
    }];
}

- (void)saveGetInbackgroundOnSuccess:(ABSaveSuccessBlock)successBlock onFailureBlock:(ABFailureBlock)failureBlock
{
    //网络断了 则不能保存
    AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager sharedManager];
    [manager startMonitoring];
    [manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        
        switch (status) {
            case AFNetworkReachabilityStatusNotReachable:
                [JQProgressHUD showErrorMessage:kNetWorkingString];
                break;
            case AFNetworkReachabilityStatusReachableViaWWAN:
            case AFNetworkReachabilityStatusReachableViaWiFi:
            {
                SEExcute *excute = [[SEExcute alloc] init];
                NSString *path = [NSString stringWithFormat:@"%@%@", HOST_API, self.className];
                
                [excute excuteGetWithPath:path withParam:self.saveDataDic onSuccess:^(NSDictionary *dataDic) {
                    self.saveDataDic = [NSMutableDictionary dictionaryWithDictionary:dataDic];
                    successBlock(self);
                } onFailure:^(NSError *error) {
                    failureBlock(error);
                }];
            }
                
            default:
                break;
        }
    }];
}

@end
